<template>
	<el-dialog v-model="visible" :title="!dataForm.id ? '新增' : '修改' " :close-on-click-modal='false'
		:close-on-press-escape='false' :fullscreen='true'>
		<el-form :inline="true" :model="dataForm" :rules="dataRule" ref="dataFormRef" @keyup.enter.native="dataFormSubmitHandle()"
			label-width="120px">

			<el-form-item label="盘点号" prop="checkkey">
				<el-input v-model="dataForm.checkkey" placeholder="盘点号" disabled></el-input>
			</el-form-item>
			<el-form-item label="状态" prop="status">
				<!-- <el-input v-model="dataForm.status" placeholder="状态" disabled></el-input> -->
				<el-select v-model="dataForm.status" placeholder="请选择" disabled>
					<el-option v-for="item in checkStatus" :key="item.list" :label="item.listname"
						:value="reString(item.list)">
					</el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="自物料" prop="fromsku">
				<el-input v-model="dataForm.fromsku" placeholder="自物料"></el-input>
			</el-form-item>
			<el-form-item label="至物料" prop="tosku">
				<el-input v-model="dataForm.tosku" placeholder="至物料"></el-input>
			</el-form-item>
			<el-form-item label="自区" prop="fromzone">
				<el-input v-model="dataForm.fromzone" placeholder="自区"></el-input>
			</el-form-item>
			<el-form-item label="至区" prop="tozone">
				<el-input v-model="dataForm.tozone" placeholder="至区"></el-input>
			</el-form-item>
			<el-form-item label="自库位" prop="fromloc">
				<el-input v-model="dataForm.fromloc" placeholder="自库位"></el-input>
			</el-form-item>
			<el-form-item label="至库位" prop="toloc">
				<el-input v-model="dataForm.toloc" placeholder="至库位"></el-input>
			</el-form-item>
			<el-form-item label="盘点开始时间" prop="begintime">
				<el-input v-model="dataForm.begintime" placeholder="盘点开始时间"></el-input>
			</el-form-item>
			<el-form-item label="盘点结束时间" prop="endtime">
				<el-input v-model="dataForm.endtime" placeholder="盘点结束时间"></el-input>
			</el-form-item>
			<el-form-item label="备注" prop="remark">
				<el-input v-model="dataForm.remark" placeholder="备注"></el-input>
			</el-form-item>
			<el-form-item label="回传状态" prop="backstatus">
				<el-input v-model="dataForm.backstatus" placeholder="回传状态"></el-input>
			</el-form-item>
			<el-form-item label="回传时间" prop="backdate">
				<el-input v-model="dataForm.backdate" placeholder="回传时间"></el-input>
			</el-form-item>
			<el-form-item label="回传结果" prop="backresult">
				<el-input v-model="dataForm.backresult" placeholder="回传结果"></el-input>
			</el-form-item>
			<el-form-item label="回传码" prop="backcode">
				<el-input v-model="dataForm.backcode" placeholder="回传码"></el-input>
			</el-form-item>
			<el-form-item label="仓库" prop="warehouse">
				<el-input v-model="dataForm.warehouse" placeholder="仓库"></el-input>
			</el-form-item>
			<el-form-item label="创建人" prop="adduser">
				<el-input v-model="dataForm.adduser" placeholder="创建人" disabled></el-input>
			</el-form-item>
			<el-form-item label="创建时间" prop="addtime">
				<el-input v-model="dataForm.addtime" placeholder="创建时间" disabled></el-input>
			</el-form-item>
			<el-form-item label="修改人" prop="updatuser">
				<el-input v-model="dataForm.updatuser" placeholder="修改人" disabled></el-input>
			</el-form-item>
			<el-form-item label="修改时间" prop="updatetime">
				<el-input v-model="dataForm.updatetime" placeholder="修改时间" disabled></el-input>
			</el-form-item>
		</el-form>
		<template #footer>
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmitHandle()">确定</el-button>
		</template>
	</el-dialog>
</template>

<script>
	import {
		ref,
		reactive,
		onMounted,
		nextTick,
		computed
	} from 'vue';
	import {
		getCheckById,
		putCheck,
		postCheck
	} from "@/network/lswms/check/Check"
	import {
		ElMessage
	} from 'element-plus'
	import _ from 'lodash'
	import {
		getSequenceByType
	} from "@/network/lswms/base/Sequence.js"
	import {
		getCodeListByCode
	} from "@/network/lswms/base/Codelist.js"

	export default {
		name: "checkAddOrUpdate",
		emits: ["refreshDataList"],
		setup(props, {
			emit
		}) {
			const visible = ref(false);
			const dataForm = ref({
				id: '',
				checkkey: '',
				status: 0,
				fromsku: '',
				tosku: '',
				fromzone: '',
				tozone: '',
				fromloc: '',
				toloc: '',
				begintime: '',
				endtime: '',
				remark: '',
				backstatus: '',
				backdate: '',
				backresult: '',
				backcode: '',
				warehouse: '',
				adduser: '',
				addtime: '',
				updatuser: '',
				updatetime: ''
			});


			const dataRule = reactive({
				/*     checkkey: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          status: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromsku: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tosku: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromzone: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          tozone: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          fromloc: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          toloc: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          begintime: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          endtime: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          remark: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          backstatus: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          backdate: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          backresult: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          backcode: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          warehouse: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          adduser: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          addtime: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          updatuser: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ],          updatetime: [
            { required: true, message: '必填项不能为空', trigger: 'blur' }
          ]  */
			})

			const dataFormRef = ref(null)

			const init = () => {
				visible.value = true;
				checkStatusFn();
				nextTick(function() {
					dataFormRef.value.resetFields();
					if (dataForm.value.id) {
						getcheckInfo();
					} else {
						checkkeyFn();
					}
				})
			}

			const getcheckInfo = () => {
				getCheckById(dataForm.value.id).then(function(res) {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value = _.assignIn({}, dataForm.value, res.data);
				}).catch((err) => {});
			}
			//获取盘点号
			const checkkeyFn = () => {
				getSequenceByType('check').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					dataForm.value.checkkey = res.data;
				}).catch((err) => {});
			}

			const checkStatus = ref("");

			const checkStatusFn = () => {

				getCodeListByCode('CheckStatus').then((res) => {
					if (res.code !== 0) {
						return ElMessage.error(res.msg);
					}
					checkStatus.value = res.data;
				}).catch((err) => {});
			}


			// 表单提交
			const dataFormSubmitHandle = _.debounce(() => {
				dataFormRef.value.validate(function(valid) {
					if (!valid) {
						return false;
					}
					let qing = () => {
						return !dataForm.value.id ? postCheck(dataForm.value) : putCheck(dataForm.value)
					}
					qing().then(function(res) {
						if (res.code !== 0) {
							return ElMessage.error(res.msg);
						}
						ElMessage({
							message: '操作成功',
							type: 'success',
							duration: 500,
							onClose: function() {
								visible.value = false
								emit('refreshDataList')
							}
						})
					}).catch(function() {});
				})
			}, 1000, {
				'leading': true,
				'trailing': false
			})
			return {
				visible,
				dataForm,
				dataRule,
				init,
				dataFormSubmitHandle,
				dataFormRef,
				checkkeyFn,
				checkStatus

			}
		},
		methods: {
			//hasPermission
			reString: function(val) {
				let n = Number(val)
				if (!isNaN(n)) {
					return n
				} else {
					return val
				}
			}
		}
	}
</script>

<style scoped>

</style>
